#coding=utf-8
from weibopy.auth import OAuthHandler,BasicAuthHandler
from weibopy.api import API
import re

class SinaWeibo:

    def __init__(self,username,password):
        self.username = username
        self.password = password
        self.source ="2666786167" 

    def getAtt(self, key):
        try:
            return self.obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''
        
    def getAttValue(self, obj, key):
        try:
            return obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''

    def basicAuth(self):
        source = self.source
        self.auth = BasicAuthHandler(self.username, self.password)
        self.api = API(self.auth,source=source)
        
    def setToken(self, token, tokenSecret):
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        self.auth.setToken(token, tokenSecret)
        self.api = API(self.auth)

    def parse_timeline(self,timeline):
        r = re.compile(r"(http://[^ ]+)")
        result = []
        for line in timeline:
            self.obj = line
            item ={}
            user = self.getAtt("user")
            item['mid'] = self.getAtt("id")
            item['text'] = r.sub(r'<a href="\1">\1</a>', self.getAtt("text")) 
            item['pic'] = self.getAtt("thumbnail_pic")
            item['author_name']= user.screen_name
            item['author_id'] = user.id
            item['author_domain'] = user.domain
            item['author_profile_image']= user.profile_image_url
            item['created_at'] = self.getAtt('created_at')
            item['source'] = self.getAtt('source')
            item['retweeted_status'] = self.getAtt('retweeted_status')
            result.append(item)
        return result

    def get_myself(self):
        myself = self.api.get_user(id=1500210523)
        #myself = self.api.get_user(user_id=self.auth.get_username)
        self.obj = myself
        user={}
        user['profile_image_url'] = self.getAtt('profile_image_url')
        user['name']=self.getAtt("screen_name")
        user['description']=self.getAtt("description")
        use = self.auth.get_username()
        return user


            
    def user_timeline(self):
        timeline = self.api.user_timeline(count=10, page=1)
        result = self.parse_timeline(timeline)
        return result
    
    def parse_trends_statuses(self,trends_statuses):
        result = []
        for trend in trends_statuses:
            self.obj = trend
            item ={}
            user = self.getAtt("user")
            item['mid'] = self.getAtt("id")
            item['text'] = self.getAtt("text")
            item['pic'] = self.getAtt("thumbnail_pic")
            item['author_name']= user.screen_name
            item['author_id'] = user.id
            item['author_domain'] = user.domain
            item['author_profile_image']= user.profile_image_url
            item['created_at'] = self.getAtt('created_at')
            item['source'] = self.getAtt('source')
            item['retweeted_status'] = self.getAtt('retweeted_status')
            result.append(item)
        return result
    
    def trends_statuses(self,message):
        message = message.encode("utf-8")
        trends_statuses=self.api.trends_statuses(message)
        result = self.parse_trends_statuses(trends_statuses)
        return result
    
    def parse_user_profile(self,user_prfl):
        result = []
        self.obj = user_prfl
        item ={}
        item['uid'] = self.getAtt("id")
        item['userName'] = self.getAtt("name")
        item['screenName'] = self.getAtt("screenName")
        result.append(item)
        return result
    
    def user_profile(self):
        user_prfl=self.api.verify_credentials()
        result = self.parse_user_profile(user_prfl)
        return result
        
